#include "app_buzzer.h"
#include "main.h"

// 全局蜂鸣器实例
app_buzzer_t app_buzzer = {0};

// 预设提示音数据
static uint16_t success_melody[] = {NOTE_C5, NOTE_E5, NOTE_G5};
static uint16_t success_timing[] = {100, 100, 200};
static uint16_t success_length = 3;

static uint16_t error_melody[] = {NOTE_C3, NOTE_C3};
static uint16_t error_timing[] = {500, 500};
static uint16_t error_length = 2;

static uint16_t warning_melody[] = {NOTE_A4, NOTE_A4, NOTE_A4};
static uint16_t warning_timing[] = {200, 200, 200};
static uint16_t warning_length = 3;

static uint16_t alarm_melody[] = {NOTE_C5, NOTE_C5};
static uint16_t alarm_timing[] = {200, 200};
static uint16_t alarm_length = 2;

static uint16_t key_beep_melody[] = {NOTE_C6};
static uint16_t key_beep_timing[] = {50};
static uint16_t key_beep_length = 1;

static uint16_t boot_melody[] = {NOTE_C4, NOTE_E4, NOTE_G4, NOTE_C5};
static uint16_t boot_timing[] = {150, 150, 150, 300};
static uint16_t boot_length = 4;

// ==================== 基础控制函数 ====================

buzzer_err_e app_buzzer_init(void)
{
    buzzer_err_e ret = BUZZER_ERR_OK;
    
    // 检查是否已经初始化
    if (app_buzzer.is_initialized) {
        return BUZZER_ERR_OK;
    }
    
    // 初始化配置参数
    app_buzzer.config.default_freq = NOTE_C5;      // 默认频率523Hz
    app_buzzer.config.default_volume = 50;          // 默认音量50%
    app_buzzer.config.interrupt_period = 2;         // 默认中断周期2ms
    
    // 初始化底层蜂鸣器
    app_buzzer.buzzer.fnx.pwmOut = pwm_set_freq;
    app_buzzer.buzzer.fnx.gpioOut = NULL;          // 使用PWM模式
    app_buzzer.buzzer.interruptMs = app_buzzer.config.interrupt_period;
    
    // 调用底层初始化
    ret = buzzer_init(&app_buzzer.buzzer);
    if (ret != BUZZER_ERR_OK) {
        app_buzzer.state = APP_BUZZER_STATE_ERROR;
        return ret;
    }
    
    // 设置初始状态
    app_buzzer.state = APP_BUZZER_STATE_IDLE;
    app_buzzer.is_initialized = 1;
    
    return BUZZER_ERR_OK;
}

void app_buzzer_deinit(void)
{
    if (app_buzzer.is_initialized) {
        buzzer_stop(&app_buzzer.buzzer);
        app_buzzer.state = APP_BUZZER_STATE_IDLE;
        app_buzzer.is_initialized = 0;
    }
}

app_buzzer_state_t app_buzzer_get_state(void)
{
    return app_buzzer.state;
}

uint8_t app_buzzer_is_playing(void)
{
    if (!app_buzzer.is_initialized) {
        return 0;
    }
    return (buzzer_is_active(&app_buzzer.buzzer) == BUZZER_IS_ACTIVE);
}

// ==================== PWM控制函数 ====================

void pwm_set_dc(uint32_t dc)
{
    uint32_t arr, comp;
    
    if (dc > 100) dc = 100;
    
    arr = __HAL_TIM_GET_AUTORELOAD(APP_PWM_TIM);
    comp = dc * arr / 100;
    __HAL_TIM_SET_COMPARE(APP_PWM_TIM, APP_PWM_CHN, comp);
}

void pwm_set_freq(uint32_t freq)
{
    uint32_t psc, arr, sys;
    
    if (freq > 0) {
        sys = HAL_RCC_GetSysClockFreq();
        arr = __HAL_TIM_GET_AUTORELOAD(APP_PWM_TIM);
        psc = (sys / (freq * (arr + 1))) - 1;
        __HAL_TIM_SET_PRESCALER(APP_PWM_TIM, psc);
        pwm_set_dc(app_buzzer.config.default_volume);
    } else {
        __HAL_TIM_SET_COMPARE(APP_PWM_TIM, APP_PWM_CHN, 0);
    }
}

// ==================== 基础播放函数 ====================

void app_buzzer_play_freq(uint16_t freq)
{
    if (!app_buzzer.is_initialized) {
        return;
    }
    
    buzzer_turn_on(&app_buzzer.buzzer, freq);
    app_buzzer.state = APP_BUZZER_STATE_PLAYING;
}

void app_buzzer_stop(void)
{
    if (!app_buzzer.is_initialized) {
        return;
    }
    
    buzzer_stop(&app_buzzer.buzzer);
    app_buzzer.state = APP_BUZZER_STATE_IDLE;
}

void app_buzzer_beep(uint16_t freq, uint16_t duration)
{
    if (!app_buzzer.is_initialized) {
        return;
    }
    
    buzzer_start(&app_buzzer.buzzer, freq, duration, BUZZER_LOOP_OFF);
    app_buzzer.state = APP_BUZZER_STATE_PLAYING;
}

void app_buzzer_beep_loop(uint16_t freq, uint16_t period, uint8_t loop)
{
    if (!app_buzzer.is_initialized) {
        return;
    }
    
    buzzer_start(&app_buzzer.buzzer, freq, period, 
                 loop ? BUZZER_LOOP_ON : BUZZER_LOOP_OFF);
    app_buzzer.state = APP_BUZZER_STATE_PLAYING;
}

// ==================== 预设提示音函数 ====================

void app_buzzer_play_success(void)
{
    if (!app_buzzer.is_initialized) {
        return;
    }
    
    buzzer_start_array(&app_buzzer.buzzer, success_timing, success_melody, success_length);
    app_buzzer.state = APP_BUZZER_STATE_PLAYING;
}

void app_buzzer_play_error(void)
{
    if (!app_buzzer.is_initialized) {
        return;
    }
    
    buzzer_start_array(&app_buzzer.buzzer, error_timing, error_melody, error_length);
    app_buzzer.state = APP_BUZZER_STATE_PLAYING;
}

void app_buzzer_play_warning(void)
{
    if (!app_buzzer.is_initialized) {
        return;
    }
    
    buzzer_start_array(&app_buzzer.buzzer, warning_timing, warning_melody, warning_length);
    app_buzzer.state = APP_BUZZER_STATE_PLAYING;
}

void app_buzzer_play_alarm(void)
{
    if (!app_buzzer.is_initialized) {
        return;
    }
    
    buzzer_start_array(&app_buzzer.buzzer, alarm_timing, alarm_melody, alarm_length);
    app_buzzer.state = APP_BUZZER_STATE_PLAYING;
}

void app_buzzer_play_key_beep(void)
{
    if (!app_buzzer.is_initialized) {
        return;
    }
    
    buzzer_start_array(&app_buzzer.buzzer, key_beep_timing, key_beep_melody, key_beep_length);
    app_buzzer.state = APP_BUZZER_STATE_PLAYING;
}

void app_buzzer_play_boot(void)
{
    if (!app_buzzer.is_initialized) {
        return;
    }
    
    buzzer_start_array(&app_buzzer.buzzer, boot_timing, boot_melody, boot_length);
    app_buzzer.state = APP_BUZZER_STATE_PLAYING;
}

// ==================== 音乐播放函数 ====================

void app_buzzer_play_mario_theme(void)
{
    if (!app_buzzer.is_initialized) {
        return;
    }
    
    buzzer_start_array(&app_buzzer.buzzer, mario_theme_time, mario_theme_melody, mario_theme_len);
    app_buzzer.state = APP_BUZZER_STATE_PLAYING;
}

void app_buzzer_play_underworld(void)
{
    if (!app_buzzer.is_initialized) {
        return;
    }
    
    buzzer_start_array(&app_buzzer.buzzer, underworld_time, underworld_melody, underworld_len);
    app_buzzer.state = APP_BUZZER_STATE_PLAYING;
}

void app_buzzer_play_melody(uint16_t *melody, uint16_t *timing, uint16_t length)
{
    if (!app_buzzer.is_initialized || melody == NULL || timing == NULL || length == 0) {
        return;
    }
    
    buzzer_start_array(&app_buzzer.buzzer, timing, melody, length);
    app_buzzer.state = APP_BUZZER_STATE_PLAYING;
}

// ==================== 配置函数 ====================

void app_buzzer_set_default_freq(uint16_t freq)
{
    app_buzzer.config.default_freq = freq;
}

void app_buzzer_set_default_volume(uint8_t volume)
{
    if (volume > 100) volume = 100;
    app_buzzer.config.default_volume = volume;
}

void app_buzzer_set_interrupt_period(uint16_t period)
{
    if (period < 1) period = 1;
    app_buzzer.config.interrupt_period = period;
    app_buzzer.buzzer.interruptMs = period;
}

// ==================== 中断处理函数 ====================

void app_buzzer_interrupt_handler(void)
{
    if (!app_buzzer.is_initialized) {
        return;
    }
    
    buzzer_interrupt(&app_buzzer.buzzer);
    
    // 检查播放是否完成
    if (buzzer_is_active(&app_buzzer.buzzer) == BUZZER_IS_NOT_ACTIVE) {
        app_buzzer.state = APP_BUZZER_STATE_IDLE;
        app_buzzer_play_finished_callback();
    }
}

// ==================== 回调函数 ====================

void app_buzzer_play_finished_callback(void)
{
    // 默认空实现，用户可重写此函数
    // 例如：可以在这里触发其他事件或更新UI状态
}

// ==================== 底层回调重写 ====================

void buzzer_end_callback(buzzer_t *buzzer)
{
    // 调用应用层回调
    app_buzzer_play_finished_callback();
}





